__author__ = 'Knight'
import date_time
import os

def util_print(hql):
    max_len = 0
    for line in hql.split('\n'):
        if len(line) > max_len:
            max_len = len(line)
    print('+{}+'.format('-' * max_len))
    for line in hql.split('\n'):
        print('|{}{}|'.format(line, ' ' * (max_len - len(line))))
    print('+{}+'.format('-' * max_len))

def wt_array(f, array, mode=None):
    mode = mode if mode else 'w'
    p = os.path.dirname(f)
    if not os.path.exists(p):
        os.makedirs(p, exist_ok=True)
    wt = open(f, mode, encoding='utf8')
    for item in array:
        wt.write('{}\n'.format(item))
    wt.close()

def dfs_of_dir(_dir):
    if not os.path.exists(_dir):
        return []
    fs = os.listdir(_dir)
    dfs = list(map(lambda f: os.path.join(_dir, f), fs))
    return dfs

def merge_file(fs, dst):
    wt = open(dst, 'w', encoding='utf8')
    for f in fs:
        print('merge {} > {}'.format(f, dst))
        rd = open(f, 'r', encoding='utf8')
        wt.write(''.join(rd.readlines()))
        rd.close()
    wt.close()

def merge_p(_dir, dst_df):
    dfs = dfs_of_dir(_dir)
    abs_dst_df = os.path.join(_dir, dst_df)
    merge_file(dfs, abs_dst_df)

if __name__ == '__main__':
    print(date_time.time)